<?php

namespace app\admin\controller;
class Massage extends base
{

    //前置方法
    protected $beforeActionList = [
        'map'  =>  ['only' => 'index'],
    ];

    /**
     * 搜索
     */
    protected function map()
    {
        $data = $this->request->param();
        if (empty($data['cid'])) {
            $data['cid'] = 0;
        }
        $this->assign('cid', $data['cid']);
        if ($this->request->isGet()) {
            $this->datas = array();
            if (!empty($data['start_time']) && !empty($data['end_time']) && (strtotime($data['start_time']) < strtotime($data['end_time']))) {
                $this->datas['create_time'] = [
                    ['>', strtotime($data['start_time'])],
                    ['<', strtotime($data['end_time'])]
                ];
            }
            if (isset($data['status'])) {
                $this->datas['status'] = $data['status'];
            }
            if (isset($data['cid'])) {
                $this->datas['cid'] = $data['cid'];
            }
            $this->datas['lang'] = cookie('think_var_admin');
        }
    }

    public function indexall()
    {
        if ($this->request->isGet()) {
            $data = $this->request->param();
            $this->datas = array();
            if (!empty($data['start_time']) && !empty($data['end_time']) && (strtotime($data['start_time']) < strtotime($data['end_time']))) {
                $this->datas['create_time'] = [
                    ['>', strtotime($data['start_time'])],
                    ['<', strtotime($data['end_time'])]
                ];
            }
            if (isset($data['status'])) {
                $this->datas['status'] = $data['status'];
            }
            $this->datas['lang'] = cookie('think_var_admin');
        }

        $order[$this->order] = $this->sort;

        $list = $this->model->page_index($this->datas, $order)->appends($this->request->param());
        $this->assign('num', $this->model->where($this->datas)->count());
        $this->assign('list', $list);
        return $this->fetch();
    }

    //    public function edit($id)
    //    {
    //        //修改状态
    //        $this->model->where('id',$id)->update(['status'=>'1']);
    //        return parent::edit($id);
    //    }

    /**
     * 保存
     */
    public function save()
    {
        if (!$this->request->isPost()) {
            $this->error('请求失败');
        }
        $data = $this->request->post();
        $result = $this->validate($data, 'Massage');
        if (true !== $result) {
            return ajax_info($result);
        }

        return $this->update($data);
    }
}
